﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HiStaff.Control;
using HiStaff.Util;
using HiStaff.Domain;
using HiStaff.iSystem;

namespace HiStaff.UI
{
    public partial class LoadInit : Form
    {
        public DialogResult InitResult { get; set; }
        public LoadInit()
        {
            this.Font = SystemFont.FontSegoeUi;
            InitializeComponent();
        }
        public void LoadData()
        {
            if (!bgWorker.IsBusy)
                bgWorker.RunWorkerAsync();
        }
        private void LoadInit_Load(object sender, EventArgs e)
        {
            //this.SetStyle(System.Windows.Forms.ControlStyles.SupportsTransparentBackColor, true);
            //this.BackColor = System.Drawing.Color.Transparent;
            //this.TransparencyKey = System.Drawing.Color.Black;
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                int i = HiStaff.iSystem.Data.LISTTABLE.Count + 4;
                int j = 1;
                int percent = (j * 100 / i);
                bgWorker.ReportProgress(percent, "Đang tải dữ liệu danh mục...");
                Data.DATALIST = new DataSet();
                DataTable dtLst;
                foreach (var itm in HiStaff.iSystem.Data.LISTTABLE)
                {
                    j++;
                    
                    
                    dtLst = Service.ExcuteStoreToDataTable(Data.PREFIX_PROC_SELECT_LIST + itm.TABLE_NAME);
                    dtLst.TableName = itm.TABLE_NAME;
                    HiStaff.iSystem.Data.DATALIST.Tables.Add(dtLst);
                    percent = (j * 100 / i);
                    bgWorker.ReportProgress(percent, "Đang tải dữ liệu danh mục " + itm.LIST_NAME + "...");
                }

                j++;
                bgWorker.ReportProgress((j * 100 / i), "Đang tải dữ liệu Ký hiệu công...");
                int v = Data.ALLCOSIGN.Count;

                j++;
                bgWorker.ReportProgress((j * 100 / i), "Đang tải dữ liệu Danh mục lương...");
                v = Data.ALLPASIGN.Count;

                bgWorker.ReportProgress(100, "Khởi tạo hoàn thành, Đang khởi động chương trình...");
                System.Threading.Thread.Sleep(2000);
                e.Result = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
                e.Result = false;
            }
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            prbStatus.Value = e.ProgressPercentage;
            lblText.Text = e.UserState.ToString();
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            InitResult = System.Windows.Forms.DialogResult.OK;
            this.Hide();
        }
    }
}
